<?php
/**
 *  内容模型控制器
 */
namespace app\admin\controller;
use app\admin\model\ModelM as ModelMModel;
class Modelm extends Common
{
    private $model;
	private $PageCount=15;
    public function _initialize()
    {
		 parent::_initialize();
        $this->model = new ModelMModel();
    }
    // 内容模型列表
    public function index()
    {
		$modedit=false;
		$UrlParam=input();
        if (input('id') && $this->model->getModel(input('id'))) {
            $this->assign('more', true);
            $this->assign('model', $this->model->getModel(input('id')));
			$this->assign('modedit',true);
        } else {
			$UrlParam['id']='';
			$this->assign('list', true);
             if (! ! ($field = input('field')) && ! ! ($keyword = input('keyword'))) {				
                $result = $this->model->findModel($field, $keyword);
            } else {
               $result = $this->model->getList($UrlParam,$this->PageCount);
            }
            $this->assign('models', $result);
			$this->assign('UrlParam',$UrlParam);
			$this->assign('model', '');
			$this->assign('modedit',false);
        }
		
		return $this->fetch();
    }
	public function add(){
		return $this->fetch();
	}
    // 内容模型增加
    public function addsave()
    {
        if($this->request->isPost()){

			$UrlParam=input();
			
            // 获取数据
            $mcode = get_auto_code($this->model->getLastCode());
            $name = $UrlParam['name'];
            $type = $UrlParam['type'];
            $urlname = $UrlParam['urlname'];
            $listtpl = basename($UrlParam['listtpl']);
            $contenttpl = basename($UrlParam['contenttpl']);
            $status = $UrlParam['status'];
            
            if (! $name) {
                alert('模型名称不能为空！','back');
            }
            
            if ($type == 1) {
                if (!$urlname)
                    $urlname = 'about';
            } else {
                if (!$urlname)
                    $urlname = 'list';
            }
            
            if ($urlname && ! preg_match('/^[a-zA-Z0-9\-]+$/', $urlname)) {
                alert('模型URL名称只允许字母、数字、横线组成!','back');
            }
            
            if ($this->model->checkUrlname($urlname, $type)) {
                alert('模型URL名称与其他模型冲突，请换一个名称！','back');
            }
            
            if ($this->model->checkSortFilename($urlname)) {
                alert('模型URL名称与栏目URL名称冲突，请换一个名称！','back');
            }
            
            // 构建数据
            $data = array(
                'mcode' => $mcode,
                'name' => $name,
                'type' => $type,
                'urlname' => $urlname,
                'listtpl' => $listtpl,
                'contenttpl' => $contenttpl,
                'status' => $status,
                'issystem' => 0,
				'create_time' => date('Y-m-d H:i:s',time()),
				'update_time' => date('Y-m-d H:i:s',time()),
                'create_user' => session('username'),
                'update_user' => session('username')
            );
            // 执行添加
            if ($this->model->addModel($data)) {
				$this->success('新增成功！', url('/Modelm/index'));
            } else {
                $this->error('新增失败！', - 1);
            }
        }
    }

    // 内容模型删除
    public function del()
    {
        if (! $id = input('id', 'int')) {
            error('传递的参数值错误！', url('/Modelm/index'));
        }
        if ($this->model->delModel($id)) {
            $this->success('删除成功！', url('/Modelm/index'));
        } else {
            $this->error('删除失败！', url('/Modelm/index'));
        }
    }

    // 内容模型修改
    public function mod()
    {
        if (! $id = input('id', 'int')) {
            $this->error('传递的参数值错误！', url('/Modelm/index'));
        }
        // 单独修改状态
        if (($field = input('field')) && ! is_null($value = input('value'))) {
            if ($this->model->modModel($id, "$field='$value',update_user='" . session('username') . "'")) {
                location(- 1);
            } else {
                alert('修改失败！','back');
            }
        }
        $UrlParam=input();
        // 修改操作
        if($this->request->isPost()){
            // 获取数据
            $name = $UrlParam['name'];
            $type = $UrlParam['type'];
            $urlname = $UrlParam['urlname'];
            $listtpl = basename($UrlParam['listtpl']);
            $contenttpl = basename($UrlParam['contenttpl']);
            $status = $UrlParam['status'];
            
            if (! $name) {
                alert('模型名称不能为空！','back');
            }
            
            if ($type == 1) {
                if (! $urlname)
                    $urlname = 'about';
            } else {
                if (! $urlname)
                    $urlname = 'list';
            }
            if ($urlname && ! preg_match('/^[a-zA-Z0-9\-]+$/', $urlname)) {
                alert('模型URL名称只允许字母、数字、横线组成!','back');
            }
            if ($this->model->checkUrlname($urlname, $type, "id<>$id")) {
                alert('模型URL名称与其他模型冲突，请换一个名称！','back');
            }
            if ($this->model->checkSortFilename($urlname)) {
                alert('模型URL名称与栏目URL名称冲突，请换一个名称！','back');
            }
            // 构建数据
            $data = array(
                'name' => $name,
                'type' => $type,
                'urlname' => $urlname,
                'listtpl' => $listtpl,
                'contenttpl' => $contenttpl,
                'status' => $status,
                'update_user' => session('username')
            );
            
            // 执行添加
            if ($this->model->modModel($id, $data)) {
                if (! ! $backurl = input('backurl')) {
                    $this->success('修改成功！', base64_decode($backurl));
                } else {
                    $this->success('修改成功！', url('/Modelm/index'));
                }
            } else {
                location(- 1);
            }
        } else {
            // 调取修改内容
			
            $this->assign('modedit', true);
			$this->assign('list', false);
            if (! $result = $this->model->getModel($id)) {
                $this->error('编辑的内容已经不存在！', - 1);
            }
            $this->assign('model', $result);
			$this->assign('UrlParam', $UrlParam);

			return $this->fetch('index');
        }
    }
	//批量排序
    public function sort()
    {
        $SortArr=input('post.sorting/a');
		
        if(!empty($SortArr))
        {
            $TempArr=[];$SortID=[];$SortIDStr='';
            foreach ($SortArr as $key=>$val)
            {
                $SortID[]=$key;
                $TempArr[]=['id'=>$key,'sorting'=>$val];
            }
            if(!empty($SortID)){$SortIDStr=implode(',',$SortID);}
            $this->model->saveAll($TempArr);
        }
        alert('排序更新成功','back');
        exit;
    }
}